﻿using System;
using System.IO;
using GeoAPI.Geometries;

namespace NetTopologySuite.GeometriesGraph
{
	public class EdgeIntersection : IComparable
	{
		public Coordinate Coordinate
		{
			get
			{
				return this._coordinate;
			}
		}

		public int SegmentIndex
		{
			get
			{
				return this._segmentIndex;
			}
		}

		public double Distance
		{
			get
			{
				return this._distance;
			}
		}

		public EdgeIntersection(Coordinate coord, int segmentIndex, double dist)
		{
			this._coordinate = new Coordinate(coord);
			this._segmentIndex = segmentIndex;
			this._distance = dist;
		}

		public int CompareTo(object obj)
		{
			EdgeIntersection edgeIntersection = (EdgeIntersection)obj;
			return this.Compare(edgeIntersection.SegmentIndex, edgeIntersection.Distance);
		}

		public int Compare(int segmentIndex, double dist)
		{
			if (this._segmentIndex < segmentIndex)
			{
				return -1;
			}
			if (this._segmentIndex > segmentIndex)
			{
				return 1;
			}
			if (this._distance < dist)
			{
				return -1;
			}
			if (this._distance > dist)
			{
				return 1;
			}
			return 0;
		}

		public bool IsEndPoint(int maxSegmentIndex)
		{
			return (this.SegmentIndex == 0 && this.Distance == 0.0) || this.SegmentIndex == maxSegmentIndex;
		}

		public void Write(StreamWriter outstream)
		{
			outstream.Write(this.Coordinate);
			outstream.Write(" seg # = " + this.SegmentIndex);
			outstream.WriteLine(" dist = " + this.Distance);
		}

		public override string ToString()
		{
			return string.Concat(new object[]
			{
				this._coordinate,
				" seg # = ",
				this._segmentIndex,
				" dist = ",
				this._distance
			});
		}

		private readonly Coordinate _coordinate;

		private readonly int _segmentIndex;

		private readonly double _distance;
	}
}
